package com.jt.www.common.controller;

import com.jt.www.dao.mapper.endorse.SaveEndorseParamMapper;
import com.jt.www.dao.mapper.insured.InsuredUserInfoMapper;
import com.jt.www.dao.mapper.offline.OfflineEndorseMapper;
import com.jt.www.dao.mapper.offline.OfflineOrderMapper;
import com.jt.www.dao.mapper.offline.OfflinePolicyMapper;
import com.jt.www.domain.enums.common.BusinessTypeCodeEnum;
import com.jt.www.domain.po.OfflineEndorse;
import com.jt.www.domain.po.OfflineOrder;
import com.jt.www.domain.po.OfflinePolicy;
import com.jt.www.model.insured.InsuredUserInfoEntity;
import com.jt.www.model.param.endorse.SaveEndorseParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: cjw
 * @Description: 文件资源权限校验接口
 * @Date: 2020/4/16 15:05
 */
@Resource
@RestController
@RequestMapping("resAuthority")
@Api(value = "文件资源权限校验接口", tags = {"RES-CHECK-API"}, description = "查询投保单列表接口")
public class ResCheckController {

    public static final Logger log = LoggerFactory.getLogger(ResCheckController.class);
    private static final String TRUE = "true";
    private static final String FALSE = "false";
    @Autowired
    private SaveEndorseParamMapper saveEndorseParamMapper;
    @Autowired
    private InsuredUserInfoMapper insuredUserInfoMapper;
    @Autowired
    private OfflineOrderMapper offlineOrderMapper;
    @Autowired
    private OfflineEndorseMapper offlineEndorseMapper;
    @Autowired
    private OfflinePolicyMapper offlinePolicyMapper;

    @ApiOperation(value = "文件资源权限校验", notes = "文件资源权限校验")
    @PostMapping(value = "check/v1/{fileId}/{businessTypeCode}/{businessDataTag}", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    @ApiResponses({@ApiResponse(code = 200, response = String.class, message = "查询资源结果")})
    public String queryElevatorBindResources(@PathVariable("fileId") String fileId, @PathVariable("businessTypeCode") String businessTypeCode, @PathVariable("businessDataTag") String businessDataTag) {
        log.info("文件资源权限校验==> fileId:{} businessTypeCode:{} businessDataTag:{}", fileId, businessTypeCode, businessDataTag);
        BusinessTypeCodeEnum businessTypeCodeEnum = BusinessTypeCodeEnum.getEnumByCode(businessTypeCode);
        if (null == businessTypeCodeEnum) {
            return FALSE;
        }
        switch (businessTypeCodeEnum) {
            //只有批单入库了  投保单需要投保单鉴权
            case A101:
                return isHasSaveEndorseParam(fileId, businessDataTag);
            //被保险人三证合一图片鉴权
            case I001:
                return isInsuredUserOrgCertificateUrl(fileId, businessDataTag);
            //线下录入清单
            case F001:
                return isOfflinePolicyFile(fileId, businessDataTag);
            //线下录入附件
            case F002:
                return isOfflineAttachedFile(fileId, businessDataTag);
            //线下批单录入附件
            case F003:
                return isOfflineEndorseAttachedFile(fileId, businessDataTag);
            //线下录入保单的人员清单文件
            case L001:
                return isOfflinePolicyDetailFile(fileId, businessDataTag);
            //线下录入保单的佐证文件
            case F004:
                return isOfflineEvidenceFile(fileId, businessDataTag);
            //线下录入批单的佐证文件
            case F005:
                return isEndorseEvidenceFile(fileId, businessDataTag);
        }
        return TRUE;
    }

    private String isEndorseEvidenceFile(String fileId, String id) {
        try {
            OfflineEndorse offlineEndorse = offlineEndorseMapper.selectByPrimaryKey(Long.parseLong(id));
            if (null == offlineEndorse) {
                return FALSE;
            }
            //如果fileId 在该json中
            if (offlineEndorse.getEvidenceFile().contains(fileId)) {
                return TRUE;
            }
        } catch (Exception e) {
            log.info("线下录入批单佐证文件鉴权校验异常,fileId:{},businessDataTag:{}", fileId, id);
        }
        return FALSE;
    }

    private String isOfflineEvidenceFile(String fileId, String id) {
            try {
                OfflineOrder offlineOrder = offlineOrderMapper.selectByPrimaryKey(Long.parseLong(id));
                if (null == offlineOrder) {
                    return FALSE;
                }
                //如果fileId 在该json中
                if (offlineOrder.getEvidenceFile().contains(fileId)) {
                    return TRUE;
                }
            } catch (Exception e) {
                log.info("线下录入保单佐证文件鉴权校验异常,fileId:{},businessDataTag:{}", fileId, id);
            }
            return FALSE;
    }

    private String isOfflinePolicyDetailFile(String fileId, String id) {
        try {
            OfflinePolicy offlinePolicy = offlinePolicyMapper.selectByPrimaryKey(Long.parseLong(id));
            if (null != offlinePolicy && fileId.equals(offlinePolicy.getDetailFileId())) {
                return TRUE;
            }
        } catch (Exception e) {
            log.info("线下录入保单的人员清单文件鉴权校验异常,fileId:{}, id:{}", fileId, id);
        }
        return FALSE;
    }

    private String isOfflineEndorseAttachedFile(String fileId, String id) {
        try {
            OfflineEndorse offlineEndorse = offlineEndorseMapper.selectByPrimaryKey(Long.parseLong(id));
            if (null == offlineEndorse) {
                return FALSE;
            }
            //如果fileId 在该json中
            if (offlineEndorse.getAttachedFile().contains(fileId)) {
                return TRUE;
            }
        } catch (Exception e) {
            log.info("线下录入清单文件鉴权校验异常,fileId:{},businessDataTag:{}", fileId, id);
        }
        return FALSE;
    }

    /**
     * 线下录入清单
     *
     * @param fileId
     * @param id
     */
    private String isOfflinePolicyFile(String fileId, String id) {
        try {
            OfflineOrder offlineOrder = offlineOrderMapper.selectByPrimaryKey(Long.parseLong(id));
            if (null == offlineOrder) {
                return FALSE;
            }
            //如果fileId 在该json中
            if (offlineOrder.getPolicyFile().contains(fileId)) {
                return TRUE;
            }
        } catch (Exception e) {
            log.info("线下录入清单文件鉴权校验异常,fileId:{},businessDataTag:{}", fileId, id);
        }
        return FALSE;
    }

    /**
     * 线下录入附件
     *
     * @param fileId
     * @param id
     */
    private String isOfflineAttachedFile(String fileId, String id) {
        try {
            OfflineOrder offlineOrder = offlineOrderMapper.selectByPrimaryKey(Long.parseLong(id));
            if (null == offlineOrder) {
                return FALSE;
            }
            //如果fileId 在该json中
            if (offlineOrder.getAttachedFile().contains(fileId)) {
                return TRUE;
            }
        } catch (Exception e) {
            log.info("线下录入附件文件鉴权校验异常,fileId:{},businessDataTag:{}", fileId, id);
        }
        return FALSE;
    }

    /**
     * 被保险人三证合一图片鉴权
     *
     * @param fileId
     * @param id
     * @return
     */
    private String isInsuredUserOrgCertificateUrl(String fileId, String id) {
        try {
            InsuredUserInfoEntity insuredUserInfoEntity = insuredUserInfoMapper.selectByPrimaryKey(Long.parseLong(id));
            if (null == insuredUserInfoEntity) {
                return FALSE;
            }
            //如果fileId 是该被保险人的三证合一OrgCertificateUrl 返回true
            if (insuredUserInfoEntity.getOrgCertificateUrl().equals(fileId)) {
                return TRUE;
            }
        } catch (Exception e) {
            log.info("被保险人三证合一图片鉴权校验异常,fileId:{},businessDataTag:{}", fileId, id);
        }
        return FALSE;
    }

    /**
     * 人员清单校验 现在只有批单的人员清单需要自己校验
     *
     * @param fileId
     * @param id
     * @return
     */
    private String isHasSaveEndorseParam(String fileId, String id) {
        try {
            SaveEndorseParam saveEndorseParam = saveEndorseParamMapper.selectByPrimaryKey(Long.parseLong(id));
            if (null == saveEndorseParam) {
                return FALSE;
            }
            //如果fileId 是批单中的一个id号返回true
            if (saveEndorseParam.getFileId().equals(fileId) || saveEndorseParam.getOriginFileId().equals(fileId)) {
                return TRUE;
            }
        } catch (Exception e) {
            log.info("批单人员清单校验异常,fileId:{},businessDataTag:{}", fileId, id);
        }
        return FALSE;
    }

}
